
local tween = require '3rd/tween'

local balls = {}
local balls_final_radius = 8
local balls_rows         = 20
local balls_columns      = 20
local balls_final_color  = {255,255,255}

local duration = 2

local screen_w,screen_h = love.graphics.getDimensions()

timer = require "3rd/hump/timer"
timer.add(6.5,  function()
                    local s = require('engine/scene')
                    s.switch('welcome')
                end)

local function get_keys(tbl)
    local keys,len = {},0
    for k in pairs(tbl) do
        len = len + 1
        keys[len] = k
    end
    return keys
end

local easing_keys = get_keys(tween.easing)

local function pad(x, min, max)
    return math.max(math.min(x, max), min)
end

local function get_ball_target(column, row)
    local x = screen_w/2 - balls_final_radius * balls_columns + balls_final_radius * column * 2
    local y = screen_h/2 - balls_final_radius * balls_rows    + balls_final_radius * row * 2
    return {x=x, y=y, radius = balls_final_radius}
end

local function create_ball(column, row, i)
    local dur = duration + i * 0.01
    local angle = 2 * math.pi * balls_columns * balls_rows / i

    local ball = {
        x = screen_w / 2 + (screen_w) * math.cos(angle),
        y = screen_h / 2 + (screen_h) * math.sin(angle),
        radius = math.random(balls_final_radius / 2, balls_final_radius * 4),
        r = math.random(0,255),
        g = math.random(0,255),
        b = math.random(0,255)
    }

    local easing_key  = easing_keys[math.random(1, #easing_keys)]
    local easing      = tween.easing[easing_key]

    local target = get_ball_target(column, row)

    ball.tweens = {
        tween.new(dur, ball, target, easing),
        tween.new(dur, ball, {r=0,g=0,b=0}, 'inExpo')
    }

    return ball
end

function load()
    local len = 0
    for row=1, balls_rows do
    for column=1, balls_columns do
        len = len + 1
        balls[len] = create_ball(column, row, len)
    end
    end
end

function update(dt)
    timer.update(dt)
    if love.keyboard.isDown(' ') then dt = -dt end
    for i=1, #balls do
        for j=1, #balls[i].tweens do
            balls[i].tweens[j]:update(dt)
        end
    end
end

function draw()
    local ball, r,g,b
    for i=1, #balls do
        ball = balls[i]
        r,g,b = pad(ball.r, 0, 255), pad(ball.g, 0, 255), pad(ball.b, 0, 255)
        love.graphics.setColor(r,g,b)
        love.graphics.circle('fill', ball.x, ball.y, ball.radius)
        love.graphics.setColor(0,0,0)
        love.graphics.circle('line', ball.x, ball.y, ball.radius)
    end
end

local x={}
x.update = update
x.init = load
x.draw = draw
return x